package main

import (
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"time"
)

func main() {
	r := gin.Default()
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/hello", func(c *gin.Context) {
		session := sessions.Default(c)

		var t string
		v := session.Get("time")
		if v == nil {
			t = time.Now().String()
		} else {
			t = v.(string)
		}
		session.Set("time", t)

		var count int
		v = session.Get("count")
		if v == nil {
			count = 0
		} else {
			count = v.(int)
			count++
		}
		session.Set("count", count)

		err := session.Save()
		if err != nil {
			fmt.Println(err.Error())
		}

		fmt.Println(time.Now())
		fmt.Println(t, count)
		c.JSON(200, gin.H{
			"time": t,
			"count": count,
		})
	})
	err := r.Run(":8000")
	if err != nil {
		fmt.Println(err.Error())
	}
}


